
#include "LuaGlobal.h"

namespace LuaGui {
	bool LuaGlobal::IsExistLogFile()
	{
		return !GetApplication()->GetLogFilePath().empty();
	}

	std::string LuaGlobal::GetPackageDirectory()
	{
		return GetApplication()->GetPackageDirectory().u8string();
	}

	std::string LuaGlobal::GetStorageDirectory()
	{
		return GetApplication()->GetStorageDirectory().u8string();
	}
	std::string LuaGlobal::GetAssetDirectory()
	{
		return GetApplication()->GetAssetDirectory().u8string();
	}

	std::string LuaGlobal::GetPlatform()
	{
		return LuaGui::GetPlatform();
	}

	void LuaGlobal::Sleep(int ms)
	{
		std::this_thread::sleep_for(std::chrono::milliseconds(ms));
	}

	unsigned long long LuaGlobal::GetTimeStamp()
	{
		return  std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
	}

	unsigned int LuaGlobal::GetTimerInterval()
	{
		return GetApplication()->GetTimerInterval();
	}

	// path
	std::string LuaGlobal::FormatPath(std::string path)
	{
		fs::path _path = LuaGui::Utf8ToUnicode(path);
		return _path.lexically_normal().u8string();
	}

	std::string LuaGlobal::GetAbsolutePath(std::string path)
	{
		fs::path _path = LuaGui::Utf8ToUnicode(path);
		_path = _path.lexically_normal();
		return _path.is_absolute() ? fs::absolute(_path).u8string() : fs::absolute((GetApplication()->GetStorageDirectory() / _path)).u8string();
	}

	bool LuaGlobal::IsAbsolutePath(std::string path)
	{
		fs::path _path = LuaGui::Utf8ToUnicode(path);
		return _path.is_absolute();
	}

	std::string LuaGlobal::GetCurrentPath()
	{
		return FileSystem::GetCurrentPath().u8string();
	}

	bool LuaGlobal::IsDirectory(std::string path)
	{
		fs::path _path = LuaGui::Utf8ToUnicode(path);
		return FileSystem::IsDirectory(_path);
	}

	bool LuaGlobal::IsRegularFile(std::string path)
	{
		fs::path _path = LuaGui::Utf8ToUnicode(path);
		return FileSystem::IsRegularFile(_path);
	}

	std::vector<std::string> LuaGlobal::GetFileList(std::string path, bool recursive)
	{
		fs::path _path = LuaGui::Utf8ToUnicode(path);
		return FileSystem::GetFileList(_path, recursive);
	}

	bool LuaGlobal::CreateDirectory(std::string path)
	{
		fs::path _path = LuaGui::Utf8ToUnicode(path);
		return FileSystem::CreateDirectory(_path);
	}

	bool LuaGlobal::RemoveDirectory(std::string path, bool recursive)
	{
		fs::path _path = LuaGui::Utf8ToUnicode(path);
		return FileSystem::RemoveDirectory(_path, recursive);
	}

	void LuaGlobal::Rename(std::string old_path, std::string new_path)
	{
		fs::path _old_path = LuaGui::Utf8ToUnicode(old_path);
		fs::path _new_path = LuaGui::Utf8ToUnicode(new_path);
		return FileSystem::Rename(_old_path, _new_path);
	}

	bool LuaGlobal::Exists(std::string path)
	{
		fs::path _path = LuaGui::Utf8ToUnicode(path);
		return FileSystem::Exists(_path);
	}

	bool LuaGlobal::IsDevEnv()
	{
		return GetApplication()->IsDevEnv();
	}

	std::string LuaGlobal::GetClipboardText()
	{
		return GetApplication()->GetClipboardText();
	}

	bool LuaGlobal::SetClipboardText(std::string text)
	{
		return GetApplication()->SetClipboardText(text);
	}

	int LuaGlobal::GetWindowWidthHeight(lua_State* L)
	{
		luabridge::push(L, GetApplication()->GetWindowWidth());
		luabridge::push(L, GetApplication()->GetWindowHeight());
		return 2;
	}

	std::string LuaGlobal::LoadAsset(std::string filepath)
	{
		return GetApplication()->GetAssetTextByFilePath(filepath);
	}

	std::string LuaGlobal::GetScriptDirectory()
	{
		return GetApplication()->GetScriptDirectory().u8string();
	}

	std::string LuaGlobal::GetScriptTextByFilePath(std::string filepath)
	{
		return GetApplication()->GetScriptTextByFilePath(filepath);
	}

	static void ShowIME() {
		return GetApplication()->ShowIME();
	}

	static void HideIME() {
		return GetApplication()->HideIME();
	}

	static float GetWindowScale() {
		return GetApplication()->GetWindowScale();
	}

	static void SetWindowScale(float scale) {
		GetApplication()->SetWindowScale(scale);
	}

	static void Debug(std::string msg) {
		GetApplication()->Debug(msg);
	}
	static void Info(std::string msg) {
		GetApplication()->Info(msg);
	}
	static void Warn(std::string msg) {
		GetApplication()->Warn(msg);
	}
	static void Error(std::string msg) {
		GetApplication()->Error(msg);
	}
	static void StartTextInput() {
		GetApplication()->StartTextInput();
	}
	static void StopTextInput() {
		GetApplication()->StopTextInput();
	}
	static void SetTextInputRect(int x, int y, int w, int h) {
		GetApplication()->SetTextInputRect(x, y, w, h);
	}

	static bool IsServer() {
#ifdef SERVER
		return true;
#else
		return false;
#endif
	}

	static std::string& GetEnv() {
		return GetApplication()->GetEnv();
	}

	static std::string GetVirtualDirectory() {
		return GetApplication()->GetVirtualDirectory().u8string();
	}

	static std::string GetLogFilePath() {
		return GetApplication()->GetLogFilePath().u8string();
	}

	void LuaGlobal::Register(lua_State* L)
	{
		luabridge::getGlobalNamespace(L)
			.beginNamespace("LuaGui")
			.addFunction("GetDev", GetEnv)
			.addFunction("IsDevEnv", LuaGlobal::IsDevEnv)
			.addFunction("IsServer", IsServer)
			.addCFunction("GetWindowWidthHeight", LuaGlobal::GetWindowWidthHeight)
			.addFunction("GetScriptDirectory", LuaGlobal::GetScriptDirectory)
			.addFunction("GetPackageDirectory", LuaGlobal::GetPackageDirectory)
			.addFunction("GetStorageDirectory", LuaGlobal::GetStorageDirectory)
			.addFunction("GetVirtualDirectory", GetVirtualDirectory)
			.addFunction("GetAssetDirectory", LuaGlobal::GetAssetDirectory)
			.addFunction("GetLogFilePath", GetLogFilePath)
			.addFunction("GetScriptTextByFilePath", LuaGlobal::GetScriptTextByFilePath)
			.addFunction("GetPlatform", LuaGlobal::GetPlatform)
			.addFunction("GetTimeStamp", LuaGlobal::GetTimeStamp)
			.addFunction("GetTimerInterval", LuaGlobal::GetTimerInterval)
			.addFunction("Sleep", LuaGlobal::Sleep)
			// clipboard
			.addFunction("GetClipboardText", LuaGlobal::GetClipboardText)
			.addFunction("SetClipboardText", LuaGlobal::SetClipboardText)
			// text
			.addFunction("ShowIME", ShowIME)
			.addFunction("HideIME", HideIME)
			.addFunction("SetWindowScale", SetWindowScale)
			.addFunction("GetWindowScale", GetWindowScale)
			// ime
			.addFunction("SetTextInputRect", SetTextInputRect)
			.addFunction("StartTextInput", StartTextInput)
			.addFunction("StopTextInput", StopTextInput)
			// log
			.beginNamespace("Log")
			.addFunction("Debug", Debug)
			.addFunction("Info", Info)
			.addFunction("Warn", Warn)
			.addFunction("Error", Error)
			.endNamespace()

			// filesystem
			.beginNamespace("FileSystem")
			.addFunction("FormatPath", LuaGlobal::FormatPath)
			.addFunction("GetAbsolutePath", LuaGlobal::GetAbsolutePath)
			.addFunction("GetCurrentPath", LuaGlobal::GetCurrentPath)
			.addFunction("IsAbsolutePath", LuaGlobal::IsAbsolutePath)
			.addFunction("IsDirectory", LuaGlobal::IsDirectory)
			.addFunction("CreateDirectory", LuaGlobal::CreateDirectory)
			.addFunction("RemoveDirectory", LuaGlobal::RemoveDirectory)
			.addFunction("Rename", LuaGlobal::Rename)
			.addFunction("Exists", LuaGlobal::Exists)
			.addFunction("GetFileList", LuaGlobal::GetFileList)
			.addFunction("LoadAsset", LuaGlobal::LoadAsset)
			.endNamespace()

			.endNamespace();
	}
}

